/**
 * 
 */
package com.ywm8.ywm.admin.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ywm8.ywm.admin.service.MenuService;
import com.ywm8.ywm.domain.Admin;
import com.ywm8.ywm.domain.Menu;
import com.ywm8.ywm.domain.Shop;
import com.ywm8.ywm.utils.ValidateUtil;

/**
 * @author wangruishun
 * @version ywm8 1.0 2014-3-4 下午7:30:05
 */
@Controller("menuAction")
@Scope("prototype")
public class MenuAction extends ActionSupport implements ServletContextAware {

	private static final long serialVersionUID = -3313171676460102154L;
	private ServletContext sc;
	private Menu menu;
	private Shop shop;
	@Resource
	private MenuService ms;
	private String msg;
	private File picture;
	private String pictureFileName;
	private List<Menu> menus;
	private Admin admin = (Admin) ServletActionContext.getRequest()
			.getSession().getAttribute("admin");
	private HttpServletRequest request = ServletActionContext.getRequest();

	/**
	 * 是否登录
	 * 
	 * @return
	 */
	public boolean isLogin() {
		if (admin == null) {
			this.msg = "您未登陆";
			return false;
		}
		return true;
	}

	/**
	 * 显示所有菜
	 */
	public String queryMenuInfo() {

		if (isLogin()) {
			if (shop == null || shop.equals(null)) {
				shop = (Shop) ServletActionContext.getRequest().getSession()
						.getAttribute("shop");
			}
			Integer smid;
			smid = shop.getId();
			if (smid == null)
				smid = menu.getShopId();
			String hql = "from Menu mu where mu.aid = ? and mu.shopId=? and mu.isvalid=1";
			menus = ms.findEntityByHQL(hql, admin.getId(), smid);
			return SUCCESS;
		} else
			return "notLogin";
	}

	public String queryMenuInfo2() {

		if (isLogin()) {
			// if (shop == null || shop.equals(null)) {
			// shop = (Shop) ServletActionContext.getRequest().getSession()
			// .getAttribute("shop");
			// }
			String hql = "from Menu mu where mu.aid = ? and mu.shopId=? and mu.isvalid=1";
			menus = ms.findEntityByHQL(hql, admin.getId(), menu.getShopId());
			return SUCCESS;
		} else
			return "notLogin";
	}

	/**
	 * 添加菜
	 * 
	 * @return
	 */
	public String addMenu() {
		if (isLogin())
			return SUCCESS;
		else {
			return SUCCESS;
		}
	}

	public String addMenuInfo() {
		if (isLogin()) {
			if (ValidateUtil.isValid(pictureFileName)) {
				String dir = sc.getRealPath("/upload");
				long l = System.nanoTime();
				String ext = pictureFileName.substring(pictureFileName
						.lastIndexOf("."));
				File newFile = new File(dir, l + ext);
				picture.renameTo(newFile);
				admin = (Admin) ServletActionContext.getRequest().getSession()
						.getAttribute("admin");
				menu.setAid(admin.getId());
				menu.setShopId(shop.getId());
				menu.setPicturePath("/upload/" + l + ext);
				ms.saveEntity(menu);
				request.getSession().setAttribute("shop", shop);
				return SUCCESS;
			} else {
				this.msg = "没有添加图片哦";
				return "wrong";
			}
		}
		return "wrong";
	}

	/**
	 * 编辑菜
	 */
	public String editMenu() {
		if (isLogin()) {
			if (shop == null || shop.equals(null)) {
				shop = (Shop) ServletActionContext.getRequest().getSession()
						.getAttribute("shop");
			}
			menu = ms.getEntity(menu.getId());
			request.getSession().setAttribute("shop", shop);
			return SUCCESS;
		} else
			return "notLogin";
	}

	public String editMenuInfoById() {
		if (isLogin()) {
			if (ValidateUtil.isValid(pictureFileName)) {
				String dir = sc.getRealPath("/upload");
				long l = System.nanoTime();
				String ext = pictureFileName.substring(pictureFileName
						.lastIndexOf("."));
				File newFile = new File(dir, l + ext);
				picture.renameTo(newFile);
				admin = (Admin) ServletActionContext.getRequest().getSession()
						.getAttribute("admin");
				menu.setAid(admin.getId());
				menu.setPicturePath("/upload/" + l + ext);
				ms.saveOrUpdateEntity(menu);
				// request.getSession().setAttribute("shop", shop);
				return SUCCESS;
			} else {

				admin = (Admin) ServletActionContext.getRequest().getSession()
						.getAttribute("admin");
				menu.setAid(admin.getId());
				ms.saveOrUpdateEntity(menu);
				return SUCCESS;
			}
		}
		return "notLogin";
	}

	public String deleteMenuById() {
		if (isLogin()) {
			menu=ms.getEntity(menu.getId());
			menu.setIsvalid(0);
			ms.saveOrUpdateEntity(menu);
			return SUCCESS;
		}
		return "wrong";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet
	 * .ServletContext)
	 */

	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * @param menu
	 *            the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the picture
	 */
	public File getPicture() {
		return picture;
	}

	/**
	 * @param picture
	 *            the picture to set
	 */
	public void setPicture(File picture) {
		this.picture = picture;
	}

	/**
	 * @return the pictureFileName
	 */
	public String getPictureFileName() {
		return pictureFileName;
	}

	/**
	 * @param pictureFileName
	 *            the pictureFileName to set
	 */
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	/**
	 * @return the menus
	 */
	public List<Menu> getMenus() {
		return menus;
	}

	/**
	 * @param menus
	 *            the menus to set
	 */
	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	/**
	 * @return the admin
	 */
	public Admin getAdmin() {
		return admin;
	}

	/**
	 * @return the shop
	 */
	public Shop getShop() {
		return shop;
	}

	/**
	 * @param shop
	 *            the shop to set
	 */
	public void setShop(Shop shop) {
		this.shop = shop;
	}

	/**
	 * @param admin
	 *            the admin to set
	 */
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;

	}

}
